 /* 
 * File:   cBullet.cpp
 * Author: josep
 * 
 * Created on 30 / desembre / 2010, 03:36
 */

#include "cBullet.h"

cBullet::cBullet() {
}

cBullet::cBullet(const cBullet& orig) {
}

cBullet::~cBullet() {
}

void cBullet::SetInTheAir(bool inTheAir){
    //if(IsInTheAir() && !inTheAir){
    if(!inTheAir && (sm->getCurrentState()->getId()==FIRED)){
        sm->processTransition(EXPLODE);
    }
    this->inTheAir=inTheAir;
}


void cBullet::Init(cTexture *texture, int PosX, int PosY,
                                float force, int angle, int Width,
                                int Height){
    float ang= ((float)angle)*M_PI/(float)180;
    Init(texture, PosX, PosY, force, ang, Width, Height);
}

bool cBullet::isDead(){
    return sm->getCurrentState()->getId()==DEATH;
}

void cBullet::Init(cTexture *texture, int PosX, int PosY,
                                float force, float angle, int Width,
                                int Height){
    int nFrameStates=3;

    this->force=force;
    this->angle=angle;
    
    SetNFrameStates(nFrameStates);
    SetNTilesHor(8);
    SetNTilesVer(1);
    SetFrameSeq(0);
    SetFrameRate(20);
    SetFrameState(0);

    GetImageSequencesVector()->resize(nFrameStates);
    (*(GetImageSequencesVector()))[0].resize(1);
    (*(GetImageSequencesVector()))[1].resize(6);
    (*(GetImageSequencesVector()))[2].resize(1);

    for(int i=0; i<GetImageSequencesVector()->size();i++){
        for(int j=0; j<(*(GetImageSequencesVector()))[i].size(); j++){
            (*(GetImageSequencesVector()))[i][j].resize(2);
        }
    }

    (*(GetImageSequencesVector()))[0][0][X]=0;
    (*(GetImageSequencesVector()))[0][0][Y]=0;

    (*(GetImageSequencesVector()))[1][0][X]=1;
    (*(GetImageSequencesVector()))[1][0][Y]=0;
    (*(GetImageSequencesVector()))[1][1][X]=2;
    (*(GetImageSequencesVector()))[1][1][Y]=0;
    (*(GetImageSequencesVector()))[1][2][X]=3;
    (*(GetImageSequencesVector()))[1][2][Y]=0;
    (*(GetImageSequencesVector()))[1][3][X]=4;
    (*(GetImageSequencesVector()))[1][3][Y]=0;
    (*(GetImageSequencesVector()))[1][4][X]=5;
    (*(GetImageSequencesVector()))[1][4][Y]=0;
    (*(GetImageSequencesVector()))[1][5][X]=6;
    (*(GetImageSequencesVector()))[1][5][Y]=0;
    (*(GetImageSequencesVector()))[2][0][X]=7;
    (*(GetImageSequencesVector()))[2][0][Y]=0;

    cLateralAgent::Init(texture, PosX, PosY, Width, Height);
    cBullet::Init();
}

void cBullet::on(){
    sm->processTransition(FIRE);
}


void cBullet::Init(){
    FromLogicToViewCorrespondence.resize(4);
    FromLogicToViewCorrespondence[0]=0;
    FromLogicToViewCorrespondence[1]=0;
    FromLogicToViewCorrespondence[2]=1;
    FromLogicToViewCorrespondence[3]=2;
    
    StateMachine<cBullet> *vSm = new StateMachine<cBullet>();

    for(int i=0; i<4; i++){
        vSm->addState(this);
    }
    vSm->getState(OFF)->setName("Off");
    vSm->getState(FIRED)->setName("fired");
    vSm->getState(EXPLOITING)->setName("exploiting");
    vSm->getState(DEATH)->setName("Death");

    vSm->getState(OFF)->setTransition(FIRE, vSm->getState(FIRED));

    vSm->getState(FIRED)->setTransition(EXPLODE, vSm->getState(EXPLOITING));
    vSm->getState(EXPLOITING)->setTransition(DIE, vSm->getState(DEATH));

    vSm->getState(FIRED)->addSubscription(State<cBullet>::SUBSCRIPTION_IN,
                                                   &cBullet::ChangeFramestate);
    vSm->getState(EXPLOITING)->addSubscription(State<cBullet>::SUBSCRIPTION_IN,
                                                   &cBullet::ChangeFramestate);
    vSm->getState(DEATH)->addSubscription(State<cBullet>::SUBSCRIPTION_IN,
                                                   &cBullet::ChangeFramestate);
    vSm->setInitialState(OFF);

    sm = (StateMachine<cLateralAgent> *)vSm;

    /**/
    this->SetSurfaceType(NORTH_SURFACE, BULLLET_SURFACE);
    this->SetSurfaceType(SOUTH_SURFACE, BULLLET_SURFACE);
    this->SetSurfaceType(EAST_SURFACE, BULLLET_SURFACE);
    this->SetSurfaceType(WEST_SURFACE, BULLLET_SURFACE);
     /**/
    /*
        this->SetSurfaceType(NORTH_SURFACE, SOFT_SURFACE);
        this->SetSurfaceType(SOUTH_SURFACE, SOFT_SURFACE);
        this->SetSurfaceType(EAST_SURFACE, SOFT_SURFACE);
        this->SetSurfaceType(WEST_SURFACE, SOFT_SURFACE);
     */
}

int cBullet::ChangeFramestate(int trans, int oldSt, int newSt){
    if(trans==FIRE){
            SetSpeedX(sin(angle)*force);
            SetSpeedY(cos(angle)*force);
    }else if(trans==EXPLODE){
        SetSpeedX(0);
    }else if(trans==DIE){
        this->SetSurfaceType(NORTH_SURFACE, SOFT_SURFACE);
        this->SetSurfaceType(SOUTH_SURFACE, SOFT_SURFACE);
        this->SetSurfaceType(EAST_SURFACE, SOFT_SURFACE);
        this->SetSurfaceType(WEST_SURFACE, SOFT_SURFACE);
    }
    SetFrameState(GetViewState(newSt));

    return State<cLateralAgent>::CONTINUE;
}

void cBullet::NextStep(int time){
    cAutonomousAgent::NextStep(time);
    if(sm->getCurrentState()->getId()==EXPLOITING && GetFrameSeq()==5){
        sm->processTransition(DIE);
    }
}

